package com.acme.bankapp.domain.bank;

import com.acme.bankapp.service.bank.NotEnoughFundsException;
import com.acme.bankapp.service.bank.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {

	private double overdraft;

	public CheckingAccount(int id, final double amount, final double overdraft)
			throws IllegalArgumentException {
		if (amount < 0)
			throw new IllegalArgumentException("Balance is negative");
		if (overdraft < 0)
			throw new IllegalArgumentException("Overdraft is negative");
		this.balance = amount;
		this.overdraft = overdraft;
		this.id = id;
	}

	public String toString() {
		return  + this.balance 
				+ ", overdraft is: " + overdraft;
	}

	@Override
	public void withdraw(final double amount) throws NotEnoughFundsException {
		if (this.balance >= amount) {
			this.balance -= amount;
			assert (this.balance >= 0);
			assert (this.overdraft >= 0);
			System.out.println("Successfully, " + amount
					+ " was withdrawn from account");
		} else if (this.balance + this.overdraft >= amount) {
			this.overdraft = this.balance + this.overdraft - amount;
			assert (this.balance >= 0);
			assert (this.overdraft >= 0);
			System.out.println("Successfully, " + this.balance
					+ " was withdrawn from balance, " + (amount - this.balance)
					+ " was withdrawn form overdraft");
			this.balance = 0.00;

		} else {
			assert (this.balance >= 0);
			assert (this.overdraft >= 0);
			throw new OverDraftLimitExceededException(this.balance
					+ this.overdraft);
		}
	}

	@Override
	public double maximumAmountToWithdraw() {
		return this.balance + this.overdraft;
	}

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		long temp;
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(overdraft) != Double
				.doubleToLongBits(other.overdraft))
			return false;
		return true;
	}

}
